package com.demo.thread;

/**
 * 线程通过加入setDefaultUncaughtExceptionHandler 加入捕获异常
 */
public class ThreadExceptionQuestion {

    public static void main(String[] args) throws InterruptedException {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.printf("heh:线程【%s】遇到了异常，详情信息%s:\n",Thread.currentThread().getName(),e.getMessage());
                //e.printStackTrace();
            }
        });

        Thread t2 = new Thread(()->{
            throw new RuntimeException("线程异常退出");
        },"t2");

        t2.start();
        t2.join();
        //Java Thread 是一个jvm的包装，然后由gc来回收；
        System.out.println("t2:"+t2.isAlive());
    }
}
